package com.cake.liulanxiangzhu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cake.liulanxiangzhu.exception.ServiceException;
import com.cake.liulanxiangzhu.pojo.dto.AdminAlbumDTO;
import com.cake.liulanxiangzhu.pojo.dto.AdminAlbumInsertDTO;
import com.cake.liulanxiangzhu.pojo.entity.AdminAlbum;
import com.cake.liulanxiangzhu.pojo.vo.AdminAlbumVO;
import com.cake.liulanxiangzhu.service.AdminAlbumService;
import com.cake.liulanxiangzhu.mapper.AdminAlbumMapper;
import com.cake.liulanxiangzhu.web.JsonPage;
import com.cake.liulanxiangzhu.web.ServiceCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @description 针对表【admin_album(相册)】的数据库操作Service实现
 * @createDate 2023-01-20 20:54:40
 */
@Service
public class AdminAlbumServiceImpl extends ServiceImpl<AdminAlbumMapper, AdminAlbum>
        implements AdminAlbumService {
    @Autowired
    private AdminAlbumMapper adminAlbumMapper;

    @Override
    public void addNew(AdminAlbumInsertDTO adminAlbumInsertDTO) {
        String name = adminAlbumInsertDTO.getName();
        int i = adminAlbumMapper.countByName(name);
        if (i==0){
            AdminAlbum album = new AdminAlbum();
            BeanUtils.copyProperties(adminAlbumInsertDTO, album);
            album.setCreatTime(new Date());
            album.setUpdateTime(new Date());
            adminAlbumMapper.insert(album);
        } else {
            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST, "相册名称不能重复");
        }
    }

    @Override
    public void deleteById(Integer id) {
        AdminAlbum album = adminAlbumMapper.selectById(id);
        if (album == null) {
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "数据不存在");
        }
        boolean b = super.removeById(id);
        if (!b) {
            throw new ServiceException(ServiceCode.ERR_DELETE, "系统繁忙请稍后重试");
        }
    }

    @Override
    public void updateById(AdminAlbumDTO adminAlbumDTO) {
        Integer id = adminAlbumDTO.getId();
        String dtoName = adminAlbumDTO.getName();
        AdminAlbum albumVO = adminAlbumMapper.selectById(id);
        String name = albumVO.getName();
        if (!name.equals(dtoName)) {
            int i = adminAlbumMapper.countByName(dtoName);
            if (i == 0) {
                AdminAlbum album = new AdminAlbum();
                BeanUtils.copyProperties(adminAlbumDTO, album);
                album.setUpdateTime(new Date());
                adminAlbumMapper.updateById(album);
            } else {
                throw new ServiceException(ServiceCode.ERR_BAD_REQUEST, "相册名字重复了");
            }
        } else {
            AdminAlbum album = new AdminAlbum();
            BeanUtils.copyProperties(adminAlbumDTO, album);
            album.setUpdateTime(new Date());
            adminAlbumMapper.updateById(album);
        }
    }

    @Override
    public JsonPage<AdminAlbumVO> selectAllAlbums(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<AdminAlbumVO> vos = adminAlbumMapper.selectAll();
        return JsonPage.restPage(new PageInfo<>(vos));
    }

    @Override
    public AdminAlbumVO setByAlbumId(Integer id) {
        AdminAlbum album = adminAlbumMapper.selectById(id);
        AdminAlbumVO vo = new AdminAlbumVO();
        BeanUtils.copyProperties(album, vo);
        return vo;
    }

    @Override
    public List<AdminAlbumVO> listAll() {
        List<AdminAlbumVO> vos = adminAlbumMapper.selectAll();
        return vos;
    }
}

